 #include "precompiled.h"
#include "myCBIR_Core.h"

namespace myCBIR
{
    Core::Core(const string &windowName)
    {
        cvNamedWindow(windowName.c_str(),CV_WINDOW_AUTOSIZE);
        sWindowName = windowName;
        tools = new Tools;
        frameCapture = boost::shared_ptr<FrameCapture>( new FrameCapture(5) );
        //frameCaptureThread = boost::thread(boost::bind(&FrameCapture::startCapturing, frameCapture));
        faceDetector = boost::shared_ptr<FaceDetector>( 
            new FaceDetector("haarcascade_frontalface_alt.xml",
            frameCapture.get(), false, 1.16));

        pOutputWidget = new VideoOutputWidget(sWindowName, frameCapture.get(), faceDetector.get());
        faceDetector->addController(pOutputWidget);
    }

    Core::~Core()
    {
        vector<Image>::iterator itr = vImages.begin();
        while(itr!=vImages.end())
        {
            if (itr->image)
                cvReleaseImage(& itr->image);
            itr++;
        }

        if (tools)
            delete tools;

        if (pOutputWidget)
            delete pOutputWidget;

        //frameCaptureThread.interrupt();
        //frameCaptureThread.join();

        cvDestroyWindow(sWindowName.c_str());
    }

    void Core::loadImages(const string &directory, const string &type)
    {
        
        vector<string> listFiles;
        listFiles = tools->getListOfFiles(directory+"*."+type);
        for each (string str in listFiles)
        {
            printf("File detected %s\n", str.c_str());
            addImage(directory+str);
        }
    }

    void Core::addImage(const string &name)
    {
        IplImage *img = cvLoadImage(name.c_str(),1);
        Image myimg;
        myimg.image = img;
        myimg.filename = name;
        vImages.push_back(myimg);
        printf("image %s added!\n", name.c_str());
    }
    void Core::showImage(IplImage *)
    {

    }

    void Core::computeStdDevOfImages()
    {
        vector<Image>::iterator itr = vImages.begin();
        while(itr!=vImages.end())
        {
            cv::Mat    img(itr->image);
            cv::Scalar     mean;
            cv::Scalar     stddev;
            cv::meanStdDev ( img, mean, stddev );
            uchar       mean_pxl = mean.val[0];
            uchar       stddev_pxl = stddev.val[0];
            printf( "[i] stddev for image %s:  %d bytes\n", itr->filename.c_str(),  stddev_pxl );

            itr++;
        }
    }

    void Core::loadObjectforrecognize(const string &object)
    {
        classifier.load(object);
    }

    void Core::openWebcamVideoCapture()
    {
        //capture.open(0);
    }

    void Core::closeWebcamVideoCapture()
    {
        cvReleaseCapture(&pCapture);
    }

    void Core::startCapturingVideo()
    {
        //testLoop();
        frameCapture.get()->startCapturing();
    }

    void Core::testLoop()
    {       
        cv::VideoCapture *captureDevice = new cv::VideoCapture(0);

        while(cv::waitKey(5)!='u')
        {
            cv::Mat frame;
            *captureDevice >> frame;
            // cv::flip( frame, frame, 1);
            cv::rectangle(
                frame,
                cv::Point(5, 10),
                cv::Point(20, 30),
                cv::Scalar(255, 255, 255)
                );
            cv::imshow(sWindowName.c_str(), frame);
        }
        captureDevice->release();
    }
}